from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import baozou_insert_sql
import time
import excel_spider_sql
list1  = excel_spider_sql.getConditions()
username = [""]
def baozou_novel():
    for x in range(len(list1)):
        if list1[x]["Platform"] == "暴走1":
            username = ""
            Platform = "暴走1"
        elif list1[x]["Platform"] == "暴走2":
            username = ""
            Platform = "暴走2"
        elif list1[x]["Platform"] == "暴走3":
            username = ""
            Platform = "暴走3"
        else:
            print("无符合条件的平台")
            continue
            # break
        driver = webdriver.Chrome(r'D:\Program Files\python3.63\chromedriver.exe')
        #进入暴走小说平台
        driver.get("https://cps.baozoukanshu.com/admin/login.html")
        driver.maximize_window()

        driver.implicitly_wait(15)
        #点击登录渠道
        driver.find_element_by_xpath('//div/div[1]//div[3]/a').click()
        #清理账号
        
        driver.find_element_by_xpath('//*[@id="account"]').clear()
        driver.find_element_by_xpath('//*[@id="account"]').send_keys(username)
        #清理密码
        driver.find_element_by_xpath('//*[@id="password"]').clear()
        driver.find_element_by_xpath('//*[@id="password"]').send_keys('')
        time.sleep(5)
        driver.find_element_by_xpath('//*[@id="submit_btn"]').click()
        #点击每日数据报告
        try:
            WebDriverWait(driver,15).until(
                EC.element_to_be_clickable((By.XPATH,'//div[2]/div//li[5]/a'))
            )
            driver.find_element_by_xpath('//div[2]/div//li[5]/a').click()
        except Exception:
            print("点击每日数据报告出错")

        # 日期
        try:
            time.sleep(3)
            date_time = driver.find_element_by_xpath('//tr[2]/td[1]').text

            # Number_clicks = driver.find_element_by_xpath('//tr[2]/td[3]').text
            #
            # Recharge_amount = driver.find_element_by_xpath('//tr[2]/td[5]/span').text

            Amount_share = driver.find_element_by_xpath('//tr[2]/td[6]/span').text

            baozou_insert_sql.baozou_spider_everyday_sum(date_time, Amount_share,Platform)
        except Exception:
            print("日期出错")
        driver.quit()
if __name__ == '__main__':
    baozou_novel()

